function getCate() {
    $.ajax({
        url: '/my/cate/list',
        success: function (res) {
            // if(res.code===0){

            // }
            const temp = template('table', res)
            $('tbody').html(temp)
        }
    })
}
getCate()
let index;
// 添加分类
$('.add-cate').click(function () {
    index = layui.layer.open({
        title: '添加分类',
        type: 1,
        area: ['500px', '250px'],
        content: $('#layer-form').html()
    })
})

layui.form.verify({
    cate_name: [/^[\S]{1,10}$/, '请输入1-10位非空字符串'],
    cate_alias: [/^[\w\d]{1,15}$/, '请输入1-15位数字或字母']
})

$('body').on('submit', '.add-form', function (e) {
    console.log(1);

    e.preventDefault()
    $.ajax({
        url: '/my/cate/add',
        method: 'POST',
        data: $(this).serialize(),
        success: function (res) {
            if (res.code === 0) {
                layui.layer.close(index)
                getCate()
            }
            layui.layer.msg(res.message)
        }
    })
})

$('body').on('click', '.edit', function (e) {
    index = layui.layer.open({
        title: '修改分类',
        type: 1,
        area: ['500px', '250px'],
        content: $('#edit-form').html(),
        success: () => {
            console.log($(this).attr('data-id'));
            $.ajax({
                url: '/my/cate/info',
                data: {
                    id: $(this).attr('data-id')
                },
                success: function (res) {
                    layui.form.val('edit-form', res.data)
                }
            })
        }
    })
})

$('body').on('submit', '.edit-form', function (e) {
    e.preventDefault()
    $.ajax({
        method: 'PUT',
        url: '/my/cate/info',
        data: $(this).serialize(),
        success: function (res) {
            if (res.code === 0) {
                layui.layer.close(index)
                getCate()
            }
            layui.layer.msg(res.message)
        }
    })
})

$('body').on('click', '.remove', function (e) {
    e.preventDefault()
    layui.layer.confirm('是否删除该分类？', {}, index => {
        $.ajax({
            url: '/my/cate/del?id=' + $(this).attr('data-id'),
            method: 'DELETE',
            success: function (res) {
                layui.layer.close(index)
                layui.layer.msg(res.message)
                getCate()
            }
        })
    })
})